package com.lml.leetcode;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @date 2020/3/20 16:04
 * Description:
 */
public class DemoTest01 {

    public static void main(String[] args) {
        int[] nums = {3, 2, 7, 11, 15, 6};
        int target = 9;
        int[] resultArr = twoSum(nums, target);
        for (int i : resultArr) {
            System.out.println(i);
        }
    }

    /**
     * 1.两数之和
     * https://leetcode-cn.com/problems/two-sum/
     *
     * @param nums   给定数组
     * @param target 目标值
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            int key = target - nums[i];
            for (int j = i + 1; j < nums.length; j++) {
                if (key == nums[j]) {
                    return new int[]{i, j};
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    @Test
    public void test1281() {
        System.out.println(subtractProductAndSum2(234));
        System.out.println(subtractProductAndSum2(4421));
        System.out.println(subtractProductAndSum2(8));
        System.out.println(subtractProductAndSum2(23));
    }

    /**
     * 1281. 整数的各位积和之差
     * https://leetcode.cn/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
     *
     * @param n
     * @return
     */
    public int subtractProductAndSum(int n) {
        String numStr = String.valueOf(n);
        int multiply = 1, sum = 0;
        for (int i = 0; i < numStr.length(); i++) {
            char c = numStr.charAt(i);
            int numericValue = Character.getNumericValue(c);
            multiply = multiply * numericValue;

            sum = sum + numericValue;
        }
        return multiply - sum;
    }

    public int subtractProductAndSum2(int n) {
        int product = 1, sum = 0;
        do {
            product *= n % 10;
            sum += n % 10;
            n /= 10;
        } while (n != 0);
        return product - sum;
    }

    @Test
    public void test1921() {
        System.out.println(eliminateMaximum(new int[]{1, 3, 4}, new int[]{1, 1, 1}));
        System.out.println(eliminateMaximum(new int[]{1, 1, 2, 3}, new int[]{1, 1, 1, 1}));
        System.out.println(eliminateMaximum(new int[]{3, 2, 4}, new int[]{5, 2, 3}));
        System.out.println(eliminateMaximum(new int[]{3, 5, 7, 4, 5}, new int[]{2, 3, 6, 3, 2}));
    }

    public int eliminateMaximum(int[] dist, int[] speed) {
        //算出每个怪物到达城市的时间
        int[] time = new int[dist.length];
        for (int i = 0; i < dist.length; i++) {
            time[i] = (int) Math.ceil((double) dist[i] / speed[i]);
        }
        //算出每个怪物到达城市的分钟 并由小到大排序
        List<Integer> timeList = Arrays.stream(time).boxed().sorted().collect(Collectors.toList());
        //记录消灭怪物的个数
        int deal = 0;
        do {
            //每次消灭距离城市最近的怪物 也就是第一个
            timeList.remove(0);
            deal++;
            for (int i = 0; i < timeList.size(); i++) {
                Integer min = timeList.get(i);
                if ((min = min - 1) <= 0) {
                    return deal;
                }
                timeList.set(i, min);
            }
        } while (!timeList.isEmpty());
        return deal;
    }

    @Test
    public void test4() {
        int[] num1 = new int[]{1, 2};
        int[] num2 = new int[]{3, 4};
        System.out.println(findMedianSortedArrays(num1, num2));
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        List<Integer> list1 = Arrays.stream(nums1).boxed().collect(Collectors.toList());
        List<Integer> list2 = Arrays.stream(nums2).boxed().collect(Collectors.toList());
        list1.addAll(list2);
        list1.sort(Integer::compareTo);

        int size = list1.size();
        int index = size / 2;
        if (size % 2 == 0) {
            return (double) (list1.get(index-1) + list1.get(index)) / 2;
        } else {
            return list1.get(index);
        }
    }
    
}
